From 563c32593f78948a3419a2cfe5c1deffd4bc80c0 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Sat, 20 Jul 2024 11:35:52 -0600 Subject: [PATCH] fix gpsapp copy_char_array that could leave part of a packet uninitialized. (#1301) * fix gpsapp copy_char_array that could leave part of a packet uninitialized. * tidy up a bit. * delete empty statement --- jeeps/gpsapp.cc | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/jeeps/gpsapp.cc b/jeeps/gpsapp.cc index 4b1441ed8..d56a603e1 100644 --- a/jeeps/gpsapp.cc +++ b/jeeps/gpsapp.cc @@ -125,28 +125,31 @@ char gps_save_string[GPS_ARB_LEN]; * we uppercase the string because some models (III's and 12's) react * violently to lower case data. */ -typedef enum { UpperNo = 0, UpperYes = 1 } copycase; +enum copycase { UpperNo, UpperYes }; static -void copy_char_array(UC** dst, char* src, int count, copycase mustupper) +void copy_char_array(UC** dst, const char* src, int count, copycase mustupper) { UC* d = *dst; - int ocount = count; - do { + int copied = 0; + // Copy up to count characters from the source to the desitnation. + for (int i = 0; i < count; ++i) { UC sc = *src++; if (sc == 0) { - while (count--) { - *d++ = ' '; - } break; } if (!isalnum(sc)) { continue; - } else { - *d++ = mustupper == UpperYes ? toupper(sc) : sc; } - } while (--count) ; - *dst += ocount; + *d++ = mustupper == UpperYes ? toupper(sc) : sc; + copied++; + } + // If necessary pad with space characters so that the total count + // of characters written to the destination is count. + for (int i = copied; i < count; ++i) { + *d++ = ' '; + } + *dst += count; } -- 2.30.2